﻿using Library.Core.Application.IInfrastructure;
using Library.Core.Domain.Repository;
using Library.Core.Infrastructure.Common;
using Library.Core.Infrastructure.EntityFrameworkDataAccess;
using Library.Core.Infrastructure.EntityFrameworkDataAcceSynthetic.Extension;
using Microsoft.EntityFrameworkCore;
using Synthesize.BaseSet.Domain.Aggregation.Dtos.Authority.Role;
using Synthesize.BaseSet.Domain.Aggregation.Entity.Authority;
using Synthesize.BaseSet.Domain.Repository.Authority;
using Synthesize.Infrastructure.PersistentObjects.Authority;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Synthesize.Infrastructure.Repository.Authority
{
    /// <summary>
    /// 角色基础类
    /// </summary>
    public class RoleRepository : RepositoryBase<RoleInfoEntity, RoleInfo>, IRoleRepository
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="currentUserInfo"></param>
        /// <param name="dbContext"></param>
        public RoleRepository(ICurrentUserInfo currentUserInfo, IEFContext dbContext) : base(currentUserInfo, dbContext)
        {

        }

        /// <summary>
        /// 根据角色名称获取分页信息
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="specification"></param>
        /// <param name="orderExp"></param>
        /// <param name="readOnly"></param>
        /// <returns></returns>
        public async Task<PageData<GetRoleListOutDto>> GetRolePageList(int pageIndex, int pageSize, Expression<Func<RoleInfoEntity, bool>> specification,
            (Expression<Func<GetRoleListOutDto, dynamic>>, bool)[] orderExp, bool readOnly)
        {

            var result = new PageData<GetRoleListOutDto>();

            var pageData =  GetAll(readOnly: readOnly)
                .Where(specification.ReplaceParameter<RoleInfoEntity, RoleInfo>())
                .Select(x => new GetRoleListOutDto()
                {
                    Id = x.Id,
                    CreateTime = x.CreateTime,
                    Name = x.Name,
                    Alias = x.Alias,
                    Status = x.Status,
                    Sort = x.Sort,
                    Remarks = x.Remarks,
                });
            if (orderExp.Any())
            {
                result = await pageData.ToPagedList(pageIndex, pageSize, orderExp);
            }
            else {
                result = await pageData.OrderBy(x=>x.Sort).ToPagedList(pageIndex, pageSize);
            }
            return result;
        }


        /// <summary>
        /// 根据用户Id获取所属角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="readOnly"></param>
        /// <returns></returns>
        public async Task<List<GetRoleListOutDto>> GetRoleByUserId(Guid userId,bool readOnly = true)
        {

            var roleQuery = GetAll(readOnly: readOnly).Where(x => x.Status);
            var userRoleQuery = GetAll<UserRoleRe>(readOnly: readOnly).Where(x => x.UserId == userId);

            var result =await roleQuery
                .Join(userRoleQuery, role=>role.Id, userRole=> userRole.RoleId,(role, userRole) => new GetRoleListOutDto
                { 
                    Id= role.Id,
                    Name= role.Name,
                    Alias = role.Alias,
                    Status = role.Status,
                    Remarks= role.Remarks
                }).ToListAsync();

            return result;
        }
    }
}
